let name=$.cookie("name");
$(function () {
    $("#name").click(function () {
        setName();
    }).text(name);
    if (!name) {
        setTimeout(setName,100);
    }

    $("#join").click(function () {
        window.open("{LINK_join}", "_self");
    });
    $("#create").click(function () {
        window.open("{LINK_create}", "_self");
    });

    $("#save-name").click(function () {
        $("#modal-cancel").click();
    });
    $("#modal-cancel").click(function () {
        setNameOver($("#input-name").val());
    });
    $("#input-name").keydown(function (e) {
        if (e.keyCode === 13){
            setNameOver($("#input-name").val());
        }
    })
});

function setName() {
    $("#open-modal").click();
    $("#input-name").val(name);

}
function setNameOver(newName){
    if (!newName) {
        if (!name) {
            setName();
        }
        return;
    }
    $.post("./?/setName", {newName: newName}, function (text) {
        if (text === "ok") {
            location.reload();
        } else {
            alert(text);
        }
    });
}

